var express = require('express');
var bodyParser = require('body-parser');
var db = require('./fengzhuang2.js')
var app = express();
var formidable = require('formidable'); //提供上传文件的模块
var fs=require('fs');   //提供更改名字模块
var path = require('path'); //提供获取后缀名的方法
app.use('/reg',express.static('./reg'))
app.use('/log',express.static('./log'))
app.use('/userinfo',express.static('./userinfo'))
app.use('/student',express.static('./student'))
app.use('/student/uploads',express.static('./uploads'))
app.use(bodyParser.urlencoded({extended:false}))
app.use('/zhuce',function(req,res){
    //解决跨域
    res.header('Access-Control-Allow-Origin','*');
    //获取表单提交的名字
    var name = req.body.name;
    //获取表单提交的密码
    var password = req.body.password;
    var profession = req.body.profession;
    //这是我们的数据库地址
    var names={
        name:name
    }
    var data={
        name:name,
        password:password,
        profession:profession
    };
    db.find('people',names,function(err,result){
        if(err){
            console.log(err);
        }
        if(result==''){
            db.insertOne('people',data,function(err,result){
                if (err) {
                    console.log(err)
                };
                res.send('success');
            })
        }else {
            res.send('error');
        }
    });

})
app.use('/denglu',function(req,res){
    //解决跨域
    res.header('Access-Control-Allow-Origin','*');
    //获取表单提交的名字
    var name = req.body.name;
    //获取表单提交的密码
    var password = req.body.password;
    var profession = req.body.profession;
    //这是我们的数据库地址
    var data={
        name:name,
        password:password,
        profession:profession
    }

    db.find('people',data,function(err,result){
        if(result==''){
            res.send('error')
        }else{
            if(data.profession=='student'){
                res.send('student')
            }else {
                res.send('teacher')
            }
        }
    })
})
//加载访问
app.use('/load',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    var key = req.body.key;
    var data={
        profession:'student',
    }
    db.find('people',data,function(err,result){
        if (err) {
            console.log(err)
        };
        res.send(result);
    })
})
//插入访问
app.use('/charu',function(req,res){
    //解决跨域
    res.header('Access-Control-Allow-Origin','*');
    //获取表单提交的名字
    var sex = req.body.sex;
    var name = req.body.name;
    var age = req.body.age;
    var data={
        profession:'student',
        name:name,
        sex:sex,
        age:age,
        password:'123'
    }
    var names={
        name:name
    }
    var keys={
        profession:'student'
    }
    db.find('people',names,function(err,result){
        if(result==''){
            db.insertOne('people',data,function(err,result){
                if (err) {
                    console.log(err)
                };
                db.find('people',keys,function(err,result){
                    res.send(result);
                })
            })
        }else{
            res.send('error')
        }
    })


})
//搜索访问
app.use('/search',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    //获取表单提交的名字
    var sex = req.body.sex;
    var name = req.body.name;
    var age = req.body.age;
    var data={
        name:name,
        profession:'student',
    }
    db.find('people',data,function(err,result){
        if(err){
            console.log(err);
        }
        if(result==''){
            res.send('error')
        }else{
            res.send(result);
        }
    })
})
app.use('/update',function(req,res){
    res.header('Access-Control-Allow-Origin','*');
    //获取表单提交的名字
    var oldname = req.body.oldname;
    var oldsex = req.body.oldsex;
    var oldage = req.body.oldage;
    var newname = req.body.newname;
    var newsex = req.body.newsex;
    var newage = req.body.newage;
    var data={
        profession:'student'
    }
    db.updateMany(
        "people",
        {
            name:oldname
        },
        {
            $set:{ name:newname,sex:newsex,age:newage }     //怎么改
        },
        function(err,result){   //改完之后做什么
            if(err){
                console.log(err);
            }
            db.find('people',data,function(err,result){
                if(err){
                    console.log(err);
                }
                    res.send(result);
            })
        }
    );
})
app.use('/delete',function(req,res){
    var delname = req.body.delname;
    var delsex = req.body.delsex;
    var delage = req.body.delage;
    var data= {
        name: delname
    }
    var heros={
        profession:"student"
    }
        db.deleteMany("people",data,function(err,result){
        if(err){
            console.log(err);
        }
            db.find('people',heros,function(err,result){
                if(err){
                    console.log(err);
                }
                res.send(result);
            })
    });

})
//插入头像
app.use('/headphoto',function(req,res) {
    var name = req.body.name
    var form = new formidable.IncomingForm();
    // 第二步：设置我们的上传路径
    form.uploadDir = "./uploads";
    // 第三步：设置全部上传完毕后的执行函数
    form.parse(req, function (err, fields, files) {
        var oldpath = files.tupian.path;
        var extname = path.extname(files.tupian.name);
        var newpath = oldpath + extname;
        var name = fields.name;
        fs.rename(oldpath, newpath, function (err) {
            if (err) {
                throw Error("改名失败");
            }
            db.updateMany(
                "people",
                {
                    name:name
                },
                {
                    $set:{ tupian:newpath }     //怎么改
                },
                function(err,result){   //改完之后做什么
                    if(err) {
                        console.log(err);
                    }
                }
            );
        });
    });
    fs.readFile('./hpsuccess/index.html',(err,data)=>{
        if(err){throw err;}
        res.writeHead(200,{"Content-type":"text/html;charset=utf-8"})
        res.end(data);
    })
})
app.use('/jilu',function(req,res){
    var password = req.body.password;
    var sex = req.body.sex;
    var name = req.body.name;
    var age = req.body.age;
    var hobby =  req.body['hobby[]'];
    var data= {
        name: name,
        sex: sex,
        password: password,
        hobby: hobby,
        age:age,
    }
    var names={
        name:name
    }
    db.updateMany(
        "people",
        {
            name:name
        },
        {
            $set:{ sex:sex,password:password,hobby:hobby,age:age }     //怎么改
        },
        function(err,result){   //改完之后做什么
            if(err){
                console.log(err);
            }
            db.find('people',names,function(err,result){
                if(err){
                    console.log(err);
                }
                res.send(result);
            })
        }
    );
})
app.use('/stuload',function(req,res){
    var name= req.body.name;
    var data={
        name:name,
    }
    db.find('people',data,function(err,result){
        if(err){
            console.log(err);
        }
        res.send(result);
    })
})

app.listen(3000);

